﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Net.Mime;

namespace EmaillSend
{
    public class MailContent
    {
        //private AlternateView htmlBody;

        //public AlternateView HtmlBody
        //{
        //    get { return htmlBody; }
           // set { htmlBody = value; }
       // }
        private string id;

        public string Id
        {
            get { return id; }
            //set { id = value; }
        }
        private string subject;

        public string Subject
        {
            get { return subject; }
            //set { subject = value; }
        }

        private string mailcontent;
        private List<LinkedResource> linkList = new List<LinkedResource>();

        public MailContent(string idP,string subjectP,string contentP,string imgpathP)
        {
            this.id = idP;
            this.subject = subjectP;
            this.initMailBody(contentP,imgpathP);
        }

        public MailMessage buildMessage(string mailTo)
        {
            Encoding encoding = Encoding.GetEncoding(936);

            MailMessage message = new MailMessage();

            message.SubjectEncoding = encoding;
            Random rd = new Random();
            message.Subject = this.subject + " " + rd.Next(10000, 100001).ToString();
            message.BodyEncoding = encoding;
            //Message.Body = this._body;
            message.AlternateViews.Clear();
            message.Attachments.Clear();
            message.AlternateViews.Add(buildMailBody(mailTo));
            return message;
        }

        private void initMailBody(string content, string imgpath)
        {
            //对邮件正文中的图片进行处理
            string[] images = Regex.Split(imgpath, "\\|~~\\|", RegexOptions.IgnoreCase);
            if (images != null && images.Length > 0)
            {
                for (int i = 0; i < images.Length; i++)
                {
                    if (images[i] == "")
                        continue;
                    string[] tmp = Regex.Split(images[i], "\\|~\\|", RegexOptions.IgnoreCase);
                    content = content.Replace(tmp[1], string.Format("cid:{0}", tmp[0]));

                    LinkedResource lrImage = new LinkedResource(@tmp[1], MediaTypeNames.Image.Jpeg);
                    lrImage.ContentId = tmp[0]; //此处的ContentId 对应 htmlBodyContent 内容中的 cid: ，如果设置不正确，请不会显示图片
                    linkList.Add(lrImage);
                }
            }
            this.mailcontent = content;
        }

        private AlternateView buildMailBody(string to)
        {
            string curbody = mailcontent.Replace("#EMAIL_CODE#", Utils.Basic64Ecode(to.Trim()));
            AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(curbody, null, MediaTypeNames.Text.Html);
            foreach (LinkedResource item in linkList)
            {
                htmlBody.LinkedResources.Add(item);
            }
            return htmlBody;
        }

    }
}
